/**
 * Url Validator Module.
 * Date: 2015/04/15.
 */
define([
  'languageMap'
],function(languageMap) {
  var UrlValidator;
  UrlValidator = (function() {
    // Constructor
    function UrlValidator() {
      this.urlRuleConfig = {
        url: /^([a-z0-9]+[a-z0-9\-]*\.){1,2}([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/,
        maxLength: 200
      };
      this.urlErrorTips = {
        urlNull: VAM.j('The website input cannot be empty'),
        urlMsg: VAM.j('The website you entered is not valid, please enter again'),
        urlLengthMsg: VAM.j('The website is up to 200 characters'),
        urlRepeatMsg: VAM.j('This website has been added, please enter a website'),
        urlSpecialCharMsg: VAM.j('Website contains special characters')
      };
      this.specialChar = "￥#$~`!@%^&*();'\"?><[]{}\\|,:/=+—“”‘";
    }

    // Get website list.
    UrlValidator.prototype.getWebsiteList = function ($tabUrlTable) {
      var $urlList = $tabUrlTable.find('tbody td.url-input'),
      urlArr = [],
      i, len = $urlList.length;
      for (i = 0; i < len; i++) {
        urlArr.push($urlList.eq(i).html());
      }
      return urlArr;
    };
    // Check special chartacter.
    UrlValidator.prototype.checkSpecialCharacter = function(inputValue) {
      var i = 0,
        txtLen = inputValue.length;
      for (i = 0; i < txtLen; i++) {
        if (this.specialChar.indexOf(inputValue.charAt(i)) !== -1) {
          return true;
        }
      }
      return false;
    };
    // Validate repeat url.
    UrlValidator.prototype.isRepeatUrl = function (url, listArr) {
      var listArrLen = listArr.length,
        flag = false; //不重复
      for (var i = 0; i <listArrLen; i++) {
        if (url == listArr[i]) {
          flag = true;//重复
          break;
        }
      }
      return flag;
    };
    // Get validate message.
    UrlValidator.prototype.getUrlValidatorMsg = function(url, $urlTable) {
      var msg = '';
      if ($.trim(url) !== '') {
        if (this.urlRuleConfig['url'].test(url)) {
          if (url.length <= this.urlRuleConfig['maxLength']) {
            if (this.isRepeatUrl(url, this.getWebsiteList($urlTable))) {
              //console.log( getWebsiteList($urlTable));
              msg = this.urlErrorTips['urlRepeatMsg'];
            } else {
              if (this.checkSpecialCharacter(url)) {
                msg = this.urlErrorTips['urlSpecialCharMsg'];
              } else {}
            }
          } else {
            msg = this.urlErrorTips['urlLengthMsg'];
          }
        } else {
          msg = this.urlErrorTips['urlMsg'];
        }
      }
      return msg;
    };
    // Check if url is error.
    UrlValidator.prototype.isUrlError = function (msg) {
      return (msg !== '') ? false : true;
    };
    // Url validate.
    UrlValidator.prototype.validate = function (validateUrl, $tabUrlTable) {
      var resultMsg = this.getUrlValidatorMsg(validateUrl, $tabUrlTable);
      return resultMsg;
    };

    return UrlValidator;
  })();

  return UrlValidator;
});
